Added a zoom to cursor option to PanCamera - #25285
Conversation
…enter and Cursor. Center is set as default and does not change the behavior of PanCamera. Cursor causes the zoom to be focused on the position of the cursor if the cursor is over the window, or the center of the window if the cursor is not over the window.
|
Welcome, new contributor! Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨ |
kfc35
left a comment
There was a problem hiding this comment.
Works for Mac
Math makes sense
| accumulated_mouse_scroll: Res<AccumulatedMouseScroll>, | ||
| mouse_scroll_conversion: Res<MouseScrollPixelsPerLine>, | ||
| mut query: Query<(&mut Transform, &mut PanCamera), With<Camera>>, | ||
| window: Single<&Window>, |
There was a problem hiding this comment.
wouldn't this make the controller not work when there are multiple windows?
There was a problem hiding this comment.
Yes, I believe that this system would get skipped if there was more than one window. I had thought that that was already the case though. Line 178 of the 'main' branch (190 of the new branch) would cause the system to return immediately if there was multiple cameras. I guess I was assuming that multiple windows would require multiple cameras. Is that not correct?
I think handling multiple cameras or windows would get more complicated than I am comfortable handling right now. I think that to do it justice, I would need to set up a system for the user (developer) to define relationships between cameras.
I could change the Single<&Window> to Query<...> thought, or change the Query<Camera...> to Single<...> if that would make things clearer and more consistent.
piedoom
left a comment
There was a problem hiding this comment.
No comments to add on code, looks good. I wonder if this should be added to the pan_camera example - I can see this being a common case for mouse/keyboard apps and possibly worth showcasing, but I don't have strong feelings.
Added a ZoomTarget enum to the PanCamera struct, with two variants: Center and Cursor. Center is set as default and does not change the behavior of PanCamera. Cursor causes the zoom to be focused on the position of the cursor if the cursor is over the window, or the center of the window if the cursor is not over the window.
Objective
In my experience, most CAD / GIS software zooms towards (and away from) the cursor. This PR adds this functionality as an option for the PanCamera Plugin.
Solution
Added an enum to the PanCamera Struct. Added input parameters and code to the run_pancamera_controller system, so that it computes the world distance between the center of the viewport and the cursor, then computes the difference in this distance due to the zoom and pans the camera to compensate.
Testing
I have only tested this change visually by running the PanCameraController Example with this option turned on and off. It looks correct to me.
I have also only tested it on my windows machine.
I am not sure how to test it more deeply than a visual inspection.
For reviewers to test it (visually) they would need to create a PanCamera struct and populate the zoom_target field with; ZoomTarget::Cursor (or ZoomTarget::Center to test the previous behaviour).
Showcase
Here is a short video of the new behaviour:
2026-08-03.09-42-05.mp4